#include "StdAfx.h"
#include "Hook.h"
#include <iostream>

Hook::Hook(const char *const szName, const DynamicData& val, const Element *const pElem):_HookBase(szName, val), pElem(pElem),
	listener(), setListener()
{
}
Hook::~Hook(void)
{
}

void	Hook::Set(const DynamicData& newVal)
{
	if(!setListener.empty())
	{
		//std::set<Listener>::iterator iter = setListener.begin();
		//while(iter != setListener.end())
		//	(*iter)(pElem, first, second, newVal);
	}
	second = newVal;
}
const DynamicData	Hook::Get() const
{
	if(!listener.empty())
	{
		//std::set<Listener>::const_iterator iter = listener.begin();
		//while(iter != listener.end())
		//	(*iter)(pElem, first, second, second);
	}
	DynamicData d = second;
	while(d.type == DynamicData::TYPE_FUNCTION) { //todo: sicherheitsrisiko?
		VlareHashMap vhm;
		DynamicData params;
		d = d.val.pfn(this, vhm, params);
	}
	return d;
}
void	Hook::AddGetListener(Listener l)
{
	//TODO: methode set/insert vergleichen
	listener.insert(l);
}
void	Hook::AddSetListener(Listener l)
{
	setListener.insert(l);
}
void	Hook::RemoveGetListener(Listener l)
{
	listener.erase(l);
}
void	Hook::RemoveSetListener(Listener l)
{
	setListener.erase(l);
}

Hook& Hook::operator= (const DynamicData& d)
{
	this->Set(d);
	return *this;
}
Hook& Hook::operator+= (const Listener& l)
{
	this->AddSetListener(l);
	this->AddGetListener(l);
	return *this;
}
Hook& Hook::operator-= (const Listener& l)
{
	this->RemoveSetListener(l);
	this->RemoveGetListener(l);
	return *this;
}

Hook::operator const DynamicData () const
{
	return Get();
}

Hook::operator const char* () const
{
	return (DynamicData)this->Get();
}
Hook::operator int () const
{
	return (DynamicData)this->Get();
}
Hook::operator Vloat () const
{
	return (DynamicData)this->Get();
}
Hook::operator void* () const
{
	return (DynamicData)this->Get();
}
Hook::operator VlareFunction () const
{
	return (DynamicData)this->Get();
}
